package com.example.zxw

import androidx.compose.animation.*
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.FiniteAnimationSpec
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.fmt.mvi.learn.gobal.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * @author: user
 * @date: 2023/5/4
 */

@Composable
fun TestAnimatedList() {
    Column {
//        TestAnimatedVisibility()
//        AnimatedContentTest()
        TestAnimalable()
    }
}

@Composable
fun TestAnimatedVisibility() {
    var visible by remember { mutableStateOf(true) }
    val density = LocalDensity.current

    Column(horizontalAlignment = Alignment.CenterHorizontally) {
        AnimatedVisibility(
            visible = visible,
            enter = slideInVertically(tween(3000)) {
                with(density) {
                    density.log()
                    -40.dp.roundToPx()
                }
            } + expandVertically(
                // Expand from the top.
                expandFrom = Alignment.Top
            ) + fadeIn(
                // Fade in with the initial alpha of 0.3f.
                initialAlpha = 0.3f
            ),
            exit = slideOutVertically() + shrinkVertically() + fadeOut(),
            modifier = Modifier.align(Alignment.CenterHorizontally)
        ) {
            Text(
                "Hello",
                fontSize = 20.sp
            )
        }
        Button(onClick = { /*TODO*/ }) {
            Text(text = "切换状态", modifier = Modifier.clickable {
                visible = !visible
            })
        }
    }

}

@Composable
fun TestAnimalable() {
    // Start out gray and animate to green/red based on `ok`
    val color = remember { Animatable(Color.Gray) }

    val offset = remember {
        Animatable(0f)
    }
    var ok by remember {
        mutableStateOf(true)
    }
    LaunchedEffect(ok) {
        color.animateTo(if (ok) Color.Green else Color.Red)

    }
    val coroutineScope = rememberCoroutineScope()
    Box(
        Modifier
            .fillMaxSize()
            .offset { IntOffset(0, offset.value.toInt()) }
            .background(color.value)
            .clickable {
                ok = !ok
                coroutineScope.launch {
                    offset.animateTo(offset.value + 100f)
                }
            }) {

    }
}


private const val DES =
    "Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about."

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun AnimatedContentTest() {
    Box {
        val pageOn = remember { mutableStateOf(false) }
        AnimatedContent(
            targetState = pageOn.value,
            transitionSpec = {
                fadeIn(animationSpec = tween(2220, delayMillis = 90)) +
                        scaleIn(
                            initialScale = 0.42f,
                            animationSpec = tween(2220, delayMillis = 90)
                        ) with
                        fadeOut(animationSpec = tween(2000))
            }
        ) { state ->
            Text(text = if (!state) "Empty" else "$DES")
        }

        Button(onClick = { pageOn.value = !pageOn.value }, modifier = Modifier.padding(16.dp)) {
            Text(text = if (pageOn.value) "隐藏内容" else "显示内容")
        }
    }
}